package com.zzxy.spring5.factory;

import aspect.MyAspect;
import com.zzxy.spring5.service.impl.StudentServiceImpl;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MyBeanFactoryCglib {


    public static Object createStudentService() {
        StudentServiceImpl studentService = new StudentServiceImpl();
        MyAspect aspect = new MyAspect();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(studentService.getClass());
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                aspect. ();
                Object obj = methodProxy.invokeSuper(o, objects);
                aspect.aftere();
                return obj;
            }
        });
//        return null;
        Object studentServiceProxCglib = (StudentServiceImpl) enhancer.create();
        return studentServiceProxCglib;
    }
}




